package cn.lmxdev.ptms.service.impl;

import cn.lmxdev.ptms.bean.exception.BizException;
import cn.lmxdev.ptms.bean.table.TStudentSubmit;
import cn.lmxdev.ptms.dao.IStudentSubmitDao;
import cn.lmxdev.ptms.service.IStudentSubmitServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class StudentSubmitServletImpl implements IStudentSubmitServlet {
    private final IStudentSubmitDao studentSubmitDao;

    @Autowired
    public StudentSubmitServletImpl(
            IStudentSubmitDao studentSubmitDao
    ) {
        this.studentSubmitDao = studentSubmitDao;
    }

    @Override
    @Transactional
    public TStudentSubmit get(Long id) {
        return this.studentSubmitDao.find(id);
    }

    @Override
    @Transactional
    public long add(TStudentSubmit studentSubmit) {
        // 插入数据
        if (!this.studentSubmitDao.insert(studentSubmit))
            throw new BizException("操作失败");

        // 返回ID
        return studentSubmit.getId();
    }

    @Override
    @Transactional
    public void remove(Long id) {
        // 删除数据
        if (!this.studentSubmitDao.delete(id))
            throw new BizException("操作失败");
    }

    @Override
    @Transactional
    public void edit(TStudentSubmit studentSubmit) {
        // 查询旧数据
        TStudentSubmit oldStudentSubmit = this.studentSubmitDao.find(studentSubmit.getId());

        // 更新属性
        oldStudentSubmit.setTitle(studentSubmit.getTitle())
                .setFulfill(studentSubmit.getFulfill())
                .setComment(studentSubmit.getComment());

        // 保存数据
        if (!this.studentSubmitDao.update(oldStudentSubmit))
            throw new BizException("操作失败");
    }

    @Override
    @Transactional
    public long count(TStudentSubmit studentSubmit) {
        return this.studentSubmitDao.count(studentSubmit);
    }

    @Override
    @Transactional
    public List<TStudentSubmit> list(Long limit, Long page, TStudentSubmit studentSubmit) {
        return this.studentSubmitDao.select(page != null ? (page - 1) * limit : null, limit, studentSubmit);
    }

    @Override
    @Transactional
    public long adviserCount(Long adviserId, TStudentSubmit studentSubmit) {
        return this.studentSubmitDao.adviserCount(adviserId, studentSubmit);
    }

    @Override
    @Transactional
    public List<TStudentSubmit> adviserList(Long adviserId, Long limit, Long page, TStudentSubmit studentSubmit) {
        return this.studentSubmitDao.adviserSelect(adviserId, page != null ? (page - 1) * limit : null, limit, studentSubmit);
    }
}
